﻿using HtmlAgilityPack;
using Spider.Entitys;
using Spider.Rules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Spider.Analyse
{
    public class 省市县规则解析 : AnalyseBase, IAnalyse
    {
        public override List<T> GetListData<T>(HtmlDocument doc, IRule rule, object o = null)
        {
            List<T> listEntity = new List<T>();
            //更加xpath获取总的对象，如果不为空，就继续选择dl标签
            HtmlNode res = doc.DocumentNode.SelectSingleNode(rule.RuleXPathString());
            if (res != null)
            {
                var list = res.SelectNodes(@"tr");//选择标签数组
                foreach (var item in list)
                {
                    var tdlist = item.SelectNodes(@"td");
                    foreach (var td in tdlist)
                    {
                        if (td != null &&
                        td.FirstChild != null &&
                        td.FirstChild.Name == "a")
                        {
                            ProvinceEntity entity = new ProvinceEntity()
                            {
                                GB11643_1999 = tdlist[0].InnerText,
                                ProvinceName = td.InnerText,
                                CityList = new List<CityEntity>(),
                                IsCity = td.InnerText.Contains("市"),
                                Level = 1,
                                Url = td.InnerHtml.GetHref()
                            };
                            //entity.Entity = (EntityBase)entity.Entity;
                            listEntity.Add((T)(object)entity);
                        }
                    }
                }
            }
            return listEntity;
        }





    }
}
